<template>
	<view class="calendar-content" v-if="showCalendar">
		<view><uni-calendar :start-date="'today'" :selected="info.selected" :showMonth="true" :lunar="true" :clearDate="true" @change="change" /></view>
	</view>
</template>

<script>
import utils from '@/static/common/utils.js';
import value from '@/static/value/value.js';
/**
 * 获取任意时间
 */
function getDate(date, AddDayCount = 0) {
	if (!date) {
		date = new Date();
	}
	if (typeof date !== 'object') {
		date = date.replace(/-/g, '/');
	}
	const dd = new Date(date);

	dd.setDate(dd.getDate() + AddDayCount); // 获取AddDayCount天后的日期

	const y = dd.getFullYear();
	const m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1; // 获取当前月份的日期，不足10补0
	const d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate(); // 获取当前几号，不足10补0
	return {
		fullDate: y + '-' + m + '-' + d,
		year: y,
		month: m,
		date: d,
		day: dd.getDay()
	};
}
export default {
	components: {},
	data() {
		return {
			showCalendar: true,
			info: {
				lunar: true,
				range: true,
				insert: false,
				selected: [],
				today: ''
			}
		};
	},
	onLoad() {
		let myDate = new Date();
		let getFullYear = myDate.getFullYear();
		let getMonth = myDate.getMonth() + 1;
		let getDay = myDate.getDate();
		let newDate = `${getFullYear}-${getMonth}-${getDay}`;
		this.today = newDate;
	},
	onReady() {
		this.$nextTick(() => {
			this.showCalendar = true;
		});
	},

	methods: {
		change(e) {
			// 判断今天明天后天
			var dates = new Date();
			var year = dates.getFullYear();
			var month = dates.getMonth() + 1;
			var day = dates.getDate();
			var nextDay = dates.getDate() + 1;
			var dayAfter = dates.getDate() + 2;
			if (month < 10) {
				month = '0' + month;
			}
			if (day < 10) {
				day = '0' + day;
			}
			if (nextDay < 10) {
				nextDay = '0' + nextDay;
			}
			if (dayAfter < 10) {
				dayAfter = '0' + dayAfter;
			}
			var nowDate = year + '-' + month + '-' + day;
			var tomorrow = year + '-' + month + '-' + nextDay;
			var afterDay = year + '-' + month + '-' + dayAfter;

			if (nowDate == e.fulldate) {
				var today = '今天';
			} else if (tomorrow == e.fulldate) {
				var today = '明天';
			} else if (afterDay == e.fulldate) {
				var today = '后天';
			} else {
				var today = '';
			}
			// 把数据存到Storage里面
			const calendar = {
				fulldate: e.fulldate,
				ncWeek: e.lunar.ncWeek,
				today: today
			};
			value.value.bustDay = calendar;
			uni.setStorage({
				data: calendar,
				key: 'fulldate',
				success() {
					uni.navigateBack({});
				}
			});
		}
	}
};
</script>

<style scoped></style>
